﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BonsaiScrum.Bus;
using BonsaiScrum.Core.Command;
using BonsaiScrum.Core.Domain;
using BonsaiScrum.Core.Infrastructure;
using BonsaiScrum.Core.Persistance;

namespace BonsaiScrum.Core.CommandHandlers
{
    public class CreateNewSprintHandler : ICommandHandler<CreateNewSprint>
    {
        private ILogger _logger;
        private IDomainRepository _domainRepository;

        public CreateNewSprintHandler(ILogger logger, IDomainRepository domainRepository)
        {
            _logger = logger;
            _domainRepository = domainRepository;
        }

        public void Handle(CreateNewSprint message)
        {
            _logger.Debug("A new Sprint starting '{0}' has been submitted.", message.Start);

            var creator = _domainRepository.GetById<User>(message.CreatorId);

            var manager = creator.GetRole<Manager>();

            var sprint = manager.CreateNewSprint(message.Start, message.End, message.Description, message.ProjectId);

            _domainRepository.Add(sprint);
        }
    }
}
